﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;
//using _180FrameWork.Common;

namespace _180FrameWork.CustControls
{
    public partial class CustMenuStrip : MenuStrip
    {
        public CustDockpanel dp { get; set; }
        //IDictionary<string, frmBase> frmDict;
        ///// <summary>
        ///// 窗体字典
        ///// </summary>
        //IDictionary<string, frmBase> FrmDict
        //{
        //    get
        //    {
        //        if (frmDict == null)
        //        {
        //            frmDict = new Dictionary<string, frmBase>();
        //        }
        //        return frmDict;
        //    }
        //}

        UnityConfigurationSection section;
        /// <summary>
        /// unity配置
        /// </summary>
        UnityConfigurationSection Section
        {
            get
            {
                if (section == null)
                {
                    section = new UnityConfigurationSection();
                    AliasElement a = new AliasElement();
                    a.Alias = "frmBase";
                    a.TypeName = "_180FrameWork.CustControls.frmBase, 180FrameWork.CustControls";
                    section.TypeAliases.Add(a);
                    ContainerElement c = new ContainerElement();
                    c.Name = "menu";
                    section.Containers.Add(c);
                }
                return section;
            }
        }
       
        IUnityContainer myContainer;
        /// <summary>
        /// unity容器
        /// </summary>
        public IUnityContainer MyContainer
        {
            get
            {
                if (myContainer == null)
                {
                    myContainer = new UnityContainer(); 
                    myContainer.LoadConfiguration(Section,"menu");
                }
                return myContainer;
            }
        }

        /// <summary>
        /// 菜单结构对象
        /// </summary>
        CustMenuStripItem root;

        public CustMenuStrip()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="root"></param>
        public void Create(CustMenuStripItem root)
        {
            this.root = root;
            foreach (CustMenuStripItem chile in root.ChildList)
            {
                AddMenu(this.Items, chile);
                RegeditType(chile);
            }
        }
        /// <summary>
        /// 添加菜单项
        /// </summary>
        /// <param name="parentitem"></param>
        /// <param name="m"></param>
        void AddMenu(ToolStripItemCollection parentitem, CustMenuStripItem m)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(m.Caption, null, TestToolStripMenuItemClick);
            item.Tag = m;
            item.Visible = m.Visible;
            item.Enabled = m.Enabled;
            parentitem.Add(item);
            foreach (CustMenuStripItem mchild in m.ChildList)
            {
                AddMenu(item.DropDownItems, mchild);
            }
        }
        /// <summary>
        /// 添加菜单项
        /// </summary>
        /// <param name="parentitem"></param>
        /// <param name="m"></param>
        //void AddMenu(MenuStrip parentitem, RingMenuStripItem m)
        //{
        //    ToolStripMenuItem item = new ToolStripMenuItem(m.Caption, null, TestToolStripMenuItemClick);
        //    item.Tag = m;
        //    item.Visible = m.Visible;
        //    item.Enabled = m.Enabled;
        //    parentitem.Items.Add(item);
        //    foreach (RingMenuStripItem mchild in m.ChildList)
        //    {
        //        AddMenu(item, mchild);
        //    }
        //}
        /// <summary>
        /// 注册窗体
        /// </summary>
        /// <param name="m"></param>
        void RegeditType(CustMenuStripItem m)
        {
            if (m.Final && m.TypeName.ToString()!="")
            {
                AliasElement a = new AliasElement();
                a.Alias = m.FormID;
                a.TypeName = m.TypeName;
                Section.TypeAliases.Add(a);
                ContainerElement c = Section.Containers["menu"];
                RegisterElement r = new RegisterElement();
                r.TypeName = "frmBase";
                r.Name = m.FormID;
                r.MapToName = m.FormID;
                //r.Lifetime.TypeName = "singleton";//单列模式
                c.Registrations.Add(r);
            }
            else
            {
                foreach (CustMenuStripItem child in m.ChildList)
                {
                    RegeditType(child);
                }
            }
        }
        /// <summary>
        /// 菜单点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TestToolStripMenuItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)sender;
            CustMenuStripItem ringMenuItem = (CustMenuStripItem)toolStripMenuItem.Tag;
            if (ringMenuItem.Final && ringMenuItem.TypeName.ToString() != "")
            {
                OpenFrm(ringMenuItem.FormID, ringMenuItem.Caption);
            }
        }
        /// <summary>
        /// 打开窗体
        /// </summary>
        /// <param name="menuid"></param>
        /// <param name="caption"></param>
        public void OpenFrm(string menuid, string caption)
        {
            if (this.dp != null)
            {
                //是否存在
                bool isExist = false;
                foreach (frmBase frm in this.dp.Documents)
                {
                    if (frm.MenuID == menuid)
                    {
                        isExist = true;
                        frm.Activate();
                    }
                }
                if (!isExist)
                {
                    frmBase frm = CreateFrm(menuid, caption);
                    frm.Show(this.dp);
                }
            }
        }
        /// <summary>
        /// 通过unity容器创建窗体
        /// </summary>
        /// <param name="menuid"></param>
        /// <param name="caption"></param>
        /// <returns></returns>
        public frmBase CreateFrm(string menuid, string caption)
        {
            frmBase frm = MyContainer.Resolve<frmBase>(menuid);
            frm.Text = caption;
            frm.WindowState = FormWindowState.Maximized;
            frm.MenuID = menuid;
            //Form frmMain = this.FindForm();
            //if (frmMain != null)
            //    frm.MdiParent = frmMain;
            return frm;
        }

        /// <summary>
        /// 窗体创建事件
        /// </summary>
        public event EventHandler FormCreated;
    }
}
